package com.dt.common.validation.parser;

import java.lang.reflect.Field;

import com.alibaba.fastjson.JSONObject;
import com.dt.common.validation.annotation.Max;
import com.dt.common.validation.definition.MaxDefinition;
import com.dt.common.validation.exception.BuildingException;
import com.dt.common.validation.impl.DefaultValidationDefinition;
import com.dt.common.validation.itf.PropertyGetter;

public class MaxDefinitionParser extends DefinitionParser {

	@Override
	public DefaultValidationDefinition parseFromAnnotation(Field field, PropertyGetter propertyGetter) {
		Max max = field.getAnnotation(Max.class);
		if (max != null) {
			MaxDefinition definition = new MaxDefinition();
			definition.setMaxValue(max.value());
			definition.setRawMsg(max.msg());

			return definition;
		}
		return null;
	}

	@Override
	public DefaultValidationDefinition parseFromJson(JSONObject metaValues) {
		Long value = metaValues.getLong("value");
		if (value == null) {
			throw new BuildingException("Max value must defined.");
		}

		MaxDefinition definition = new MaxDefinition();
		definition.setMaxValue(value);
		definition.setRawMsg(this.getMsgDefinition(metaValues, true));

		return definition;
	}

}
